import requests
import pandas as pd
session = requests.Session()


def login(token):
    url = "http://127.0.0.1:22080/kcdoc/delegateLogin"
    payload = {'token': token}
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    response = session.post(url, data=payload, headers=headers)
    data = response.text
    print(data)

def login_ly(token):
    url = "http://10.2.5.15:80/kcdoc/delegateLogin"
    payload = {'token': token}
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    response = session.post(url, data=payload, headers=headers)
    data = response.text
    print(data)


def updatye_plan(plan_ids):
    url = "http://127.0.0.1:22080/kcdoc/exportPlan/batchFreshStatu"
    headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    data = {
        "planCode": plan_ids,
        "executorName": "1"
    }
    response = session.post(url, data=data, headers=headers)
    data = response.text
    print(data)


def end_plan_funtion(plan_ids):
    url = " http://127.0.0.1:22080/kcdoc/exportPlan/batchOverPlan"
    headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    data = {
        "planCode": plan_ids,
        "reason": "计划已经结束",
        "applyNo": "14282",
        "oaCode": ""
    }
    response = session.post(url, data=data, headers=headers)
    data = response.text
    print(data)


def getLcInfo(lcbh):
    url = "http://127.0.0.1:22080/kcdoc/product/process/findByLcbhOrName?lcbh={}&pageIndex=0&pageSize=10".format(lcbh)
    headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    response = session.get(url, headers=headers)
    data = response.text
    return data


def add_lc_person(data):
    url = "http://127.0.0.1:22080/kcdoc/operations/addNodeRef"
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    response = session.post(url, json=data, headers=headers)
    data = response.text
    print(data)


def user_to_task(task_id, owner, handlerUser):
    url = "http://127.0.0.1:22080/kcdoc/activiti/kctask/updateTaskUser"
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    data = {
        "taskId": task_id,
        "owner": owner,
        "handlerUser": handlerUser
    }

    response = session.post(url, json=data, headers=headers)
    data = response.text
    print(data)


def add_plan_process(plan_id):
    url = "http://127.0.0.1:22080/kcdoc/operations/addNodeRef"
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    data = {
        "lcbh": "LC24-01070",
        "id": plan_id,
        "processIndex": 3,
        "bank": "1",
        "processStep": [
            {
                "processNo": "GX0032",
                "processName": "入库",
                "timeOut": 1,
                "ratedWorkTime": 1,
                "processRoleName": "仓库组",
                "processRole": "4",
                "workshopName": "生准工段",
                "workshopId": 54,
                "quality": 0,
                "stepId": 4074937,
                "resume": "",
                "maxTaskUser": 1,
                "maxDoingUser": 1,
                "attributeList": [],
                "stepDeviceList": []
            }
        ]
    }
    response = session.post(url, json=data, headers=headers)
    data = response.text
    print(data)

def updateFile_ly(filePath, user_no):
    url = "http://10.2.5.15:80/kcdoc/uploadFile"
    payload = {'busType': 'esign', 'busId': user_no}
    files = {'file': open(filePath, 'rb')}
    response = session.post(url, data=payload, files=files)
    data = response.text
    print(user_no + "#######" + data)


def updateFile(filePath, user_no):
    url = "http://127.0.0.1:22080/kcdoc/uploadFile"
    payload = {'busType': 'esign', 'busId': user_no}
    files = {'file': open(filePath, 'rb')}
    response = session.post(url, data=payload, files=files)
    data = response.text
    print(user_no + "#######" + data)


def deleteInstance(task_id):
    url = "http://127.0.0.1:22080/kcdoc/activiti/instance/deleteInstance"
    headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    data = {
        "instanceID": task_id,
        "reason": '误触发',
        "lang": 'zh_CN'
    }
    response = session.post(url, data=data, headers=headers)
    data = response.text
    print(data)


def getDeviceInfo():
    url = "http://127.0.0.1:22080/kcdoc/device/operator/select"
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    data = {"materialNumber": "", "name": "热压罐", "curPage": 1, "pageIndex": 1, "pageSize": 40}
    response = session.post(url, json=data, headers=headers)
    data = response.text
    return data


def addDeviceUser(materialNumber, userNo, certNumber):
    url = "http://127.0.0.1:22080/kcdoc/device/operator/add"
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    data = {"materialNumber": materialNumber, "userNo": userNo, "certType": "GH", "certNumber": certNumber,
            "issuedBy": "深圳市市场监督管理局", "validityStart": "2024-05-06", "validityEnd": "2025-06-30"}
    response = session.post(url, json=data, headers=headers)
    data = response.text
    print(data)



def bindGzPlan(gzPlanCode, gzPlanLc):
    url = "http://127.0.0.1:22080/kcdoc/syncTrigger/bindGzPlan"
    headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    data = {
        "gzPlanCode": gzPlanCode,
        "gzPlanLc": gzPlanLc
    }
    response = session.post(url, data=data, headers=headers)
    data = response.text
    print(data)
def plmFileDownload(fileId,fileVersion,busId):
    url = "http://127.0.0.1:22080/kcdoc/plm/plmFileDownload"
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    data = {"fileId":fileId,"fileVersion":fileVersion,"busId":busId,"busType":"foEco"}
    response = session.post(url, json=data, headers=headers)

    return    response.text


if __name__ == '__main__':
    data = pd.read_excel("C:\\Users\\xyh\\Desktop\\计划绑定.xlsx", sheet_name='Sheet1', usecols=['生产计划编号'])
    login('712017165048788d32c953896cc7ce4a')
    for index, row in data.iterrows():
        deleteInstance(row['生产计划编号'])
    # for e in list_map:
    #     get = e.get("planList")
    #     for e1 in get:
    #         # print(e1, e.get("lc"))
    #         bindGzPlan(e1, e.get("lc"))
